package Character;

import org.json.JSONException;
import org.json.JSONObject;

import GameTools.ydMath;
import GameTools.ydPoint;
import android.graphics.Canvas;

import com.ydDev.charateranimation.R;

public class Leg {
	BodyPart thigh, calf;

	public Leg(ydPoint position) {
		thigh = new BodyPart(position, R.drawable.gc1_leg);
		calf = new BodyPart(thigh.limb.p2, R.drawable.gc1_calf);

	/*	thigh.setFirstJoint(new ydPoint(thigh.limb.p1.x - 2,
				thigh.limb.p1.y + 30));
		thigh.setSecondJoint(new ydPoint(thigh.limb.p1.x - 2,
				thigh.limb.p2.y - 30));

		calf.setFirstJoint(new ydPoint(calf.limb.p1.x - 53,
				calf.limb.p1.y + 12.5f));
		calf.setSecondJoint(new ydPoint(calf.limb.p1.x, calf.limb.p2.y - 16));*/
		
		thigh.setFirstJoint(new ydPoint(thigh.limb.p1.x - 0.008f*thigh.skin.getWidth(),
				thigh.limb.p1.y + 0.1266f*thigh.skin.getWidth()));
		thigh.setSecondJoint(new ydPoint(thigh.limb.p1.x - 0.008f*thigh.skin.getWidth(),
				thigh.limb.p1.y + 0.873f*thigh.skin.getWidth()));

		calf.setFirstJoint(new ydPoint(calf.limb.p1.x - 0.225f*thigh.skin.getWidth(),
				calf.limb.p1.y + 0.0568f*thigh.skin.sprite.getWidth()));
		calf.setSecondJoint(new ydPoint(calf.limb.p1.x, calf.limb.p2.y - 0.927f*thigh.skin.getWidth()));

		ydPoint shift = new ydPoint(thigh.j2.x - calf.j1.x, thigh.j2.y
				- calf.j1.y);
		calf.deltaMove(shift);
	}

	public void draw(Canvas canvas) {
		thigh.draw(canvas);
		calf.draw(canvas);
	}

	public void rotatethigh(double rads, int mode) {
		if (mode == 0) {
			thigh.rotate(rads);
			calf.rotate(rads);

			ydPoint shift = new ydPoint(thigh.j2.x - calf.j1.x, thigh.j2.y
					- calf.j1.y);
			calf.deltaMove(shift);
		} else if (mode == 1) {
			calf.rotate(rads);
		}
	}

	public void deltaMove(ydPoint shift) {
		thigh.deltaMove(shift);
		calf.deltaMove(shift);
	}

	public void pointTo(ydPoint clickedAt, int i) {
		if (i == 0) {
			ydPoint currentSlope = thigh.limb.slope;
			ydPoint newSlope = ydMath.slopeOf(thigh.j1, clickedAt);

			double deltaRads = ydMath.vectorToRads(newSlope)
					- ydMath.vectorToRads(currentSlope);
			thigh.rotate(deltaRads);

			ydPoint shift = new ydPoint(thigh.j2.x - calf.j1.x, thigh.j2.y
					- calf.j1.y);
			calf.deltaMove(shift);

			// currentSlope=calf.limb.slope;
			// deltaRads=ydMath.vectorToRads(newSlope)-ydMath.vectorToRads(currentSlope);
			calf.rotate(deltaRads);
		} else if (i == 1) {
			ydPoint currentSlope = calf.limb.slope;
			ydPoint newSlope = ydMath.slopeOf(calf.j1, clickedAt);
			currentSlope = calf.limb.slope;
			double deltaRads = ydMath.vectorToRads(newSlope)
					- ydMath.vectorToRads(currentSlope);
			calf.rotate(deltaRads);
		}
	}

	public void rotate(int direction) {
		// TODO Auto-generated method stub
		if (direction == 0) {
			thigh.rotate(thigh.angularVelocity);
			calf.rotate(thigh.angularVelocity);

			ydPoint shift = new ydPoint(thigh.j2.x - calf.j1.x, thigh.j2.y
					- calf.j1.y);
			calf.deltaMove(shift);
		} else if (direction == 1) {
			thigh.rotate(-thigh.angularVelocity);
			calf.rotate(-thigh.angularVelocity);

			ydPoint shift = new ydPoint(thigh.j2.x - calf.j1.x, thigh.j2.y
					- calf.j1.y);
			calf.deltaMove(shift);
		}
	}

	public JSONObject getJSONThigh() throws JSONException {
		JSONObject json=new JSONObject();
		
		json.put("p1", thigh.limb.p1.toString());
		json.put("p2", thigh.limb.p2.toString());
		json.put("j1", thigh.j1.toString());
		json.put("j2", thigh.j2.toString());
		return json;
	}

	public JSONObject getJSONCalf() throws JSONException {
		JSONObject json=new JSONObject();
		
		json.put("p1", calf.limb.p1.toString());
		json.put("p2", calf.limb.p2.toString());
		json.put("j1", calf.j1.toString());
		json.put("j2", calf.j2.toString());
		return json;
	}

	public void loadPostion(JSONObject json) {
		JSONObject jsonThigh=new JSONObject();
		JSONObject jsonCalf=new JSONObject();
		try {
			jsonThigh=json.getJSONObject("thigh");
			jsonCalf=json.getJSONObject("calf");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		thigh.limb.setPosition(ydPoint.stringPairToydPoint(jsonThigh.optString("p1")), ydPoint.stringPairToydPoint(jsonThigh.optString("p2")));
		thigh.j1.setCoordinates(ydPoint.stringPairToydPoint(jsonThigh.optString("j1")));
		thigh.j2.setCoordinates(ydPoint.stringPairToydPoint(jsonThigh.optString("j2")));
		
		calf.limb.setPosition(ydPoint.stringPairToydPoint(jsonCalf.optString("p1")), ydPoint.stringPairToydPoint(jsonCalf.optString("p2")));
		calf.j1.setCoordinates(ydPoint.stringPairToydPoint(jsonCalf.optString("j1")));
		calf.j2.setCoordinates(ydPoint.stringPairToydPoint(jsonCalf.optString("j2")));
		
	}

	public void readjust() {
		ydPoint shift = new ydPoint(thigh.j2.x - calf.j1.x, thigh.j2.y
				- calf.j1.y);
		calf.deltaMove(shift);
		
	}
}
